
package com.android.generator.whatid;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.util.HashMap;

import com.android.replace.TextUtils;

//TODO Same method name
public class GenerateWhat {

    private static final String HANDLER_NAME = "mCalendarHandler";

    private static final boolean HANDLE_MESSAGE_HEAD = true;

    public static void main(String[] args) {

        Field[] declaredFields = CalendarManager.class.getDeclaredFields();
        int max = 0;
        for (Field field : declaredFields) {
            if (((field.getModifiers() & Modifier.FINAL) == Modifier.FINAL)
                    && ((field.getModifiers() & Modifier.STATIC) == Modifier.STATIC)
                    && ((field.getModifiers() & Modifier.PRIVATE) == Modifier.PRIVATE)) {
                field.setAccessible(true);
                try {
                    int object = (int) field.get(CalendarManager.class);
                    if (object > max) {
                        max = object;
                    }
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        //
        HashMap<String, Integer> methodMap = new HashMap<>();
        HashMap<Integer, String> idMap = new HashMap<>();
        /**
         * mCalendarHandler.obtainMessage(1,query).sendToTarget();
         */
        //Generate id;
        System.out.println("------------------- Id --------------------");
        Method[] declaredMethods = CalendarManager.class.getDeclaredMethods();
        //TODO
        int index = 0;
        for (Method method : declaredMethods) {
            if ((method.getModifiers() & Modifier.ABSTRACT) == Modifier.ABSTRACT) {
                max = max + 1;
                String idStr = "WHAT_" + method.getName().toUpperCase();
                String methodName = method.getName();
                methodMap.put(methodName, max);
                idMap.put(max, idStr);
                System.out.println("private static final int " + idStr + " = " + (max) + ";");
            }
        }
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("-------------------send to target--------------------");
        index = 0;
        for (Method method : declaredMethods) {
            if ((method.getModifiers() & Modifier.ABSTRACT) == Modifier.ABSTRACT) {
                String methodName = method.getName();
                Parameter[] parameters = method.getParameters();
                String paramterStr = "";
                for (int i = 0; i < parameters.length; i++) {
                    Parameter parameter = parameters[i];
                    paramterStr += parameter.getName();
                    if (i != parameters.length - 1 && parameters.length != 1) {
                        paramterStr = paramterStr + ",";
                    }
                }
                if (TextUtils.isEmpty(paramterStr)) {
                    System.out.println(HANDLER_NAME + ".obtainMessage(" + methodMap.get(methodName)
                            + ").sendToTarget();");
                } else {
                    System.out.println(HANDLER_NAME + ".obtainMessage(" + idMap.get(methodMap.get(methodName)) + ","
                            + "new Object[]{" + paramterStr + "}).sendToTarget();");
                }
            }
        }
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println("-------------------handleMessage--------------------");
        if (HANDLE_MESSAGE_HEAD) {
            System.out.println("        int what = msg.what;");
            System.out.println("        switch (what){");
        }
        index = 0;
        HashMap<String, String> parameterMaps = new HashMap<>();
        for (Method method : declaredMethods) {
            if ((method.getModifiers() & Modifier.ABSTRACT) == Modifier.ABSTRACT) {
                String methodName = method.getName();
                System.out.println("            case " + idMap.get(methodMap.get(methodName)) + ":");
                System.out.println("                Object[] " + methodName + " = (Object[]) msg.obj;");
                Parameter[] parameters = method.getParameters();
                for (int i = 0; i < parameters.length; i++) {
                    Parameter parameter = parameters[i];
                    String parameterTypeName = parameter.getType().getSimpleName();
                    String parameterName = parameter.getName();
                    if (parameterMaps.containsKey(parameterName)
                            && parameterMaps.get(parameterName).equals(parameterTypeName)) {
                        System.out.println("                 " + parameterName + " = (" + parameterTypeName + ") "
                                + methodName + "[" + i + "];");
                    } else {
                        System.out.println("                " + parameterTypeName + " " + parameterName + " = ("
                                + parameterTypeName + ") " + methodName + "[" + i + "];");
                    }
                    parameterMaps.put(parameterName, parameterTypeName);
                }
                System.out.println("                //TODO");
            }
        }
        if (HANDLE_MESSAGE_HEAD) {
            System.out.println("            default:");
            System.out.println("                break;");
            System.out.println("        }");
        }

    }
}
